#!/usr/bin/env python3
# Created by han on 2016/6/22
import sys, os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from module import maker,students,public
from conf import db_conf


def students_main():
    while True:
        first = ["创建学生","学生登陆"]
        user_chose = public.show(*first)
        if not user_chose:
            break
        elif user_chose == "1":
            stu_name = input("学生名字： ")
            stu_pass = input("学生密码： ")
            stu_obj = students.Students(stu_name,stu_pass)
            save_status = input("是否储存[y/n]: ")
            if save_status == "n":
                continue
            elif save_status == "y":
                save_after = public.save_obj(stu_name,stu_obj)
                if save_after:
                    msg = "储存成功"
                    public.color_msg(32,msg,40)
                else:
                    continue
        if user_chose == "2":
            while True:
                user_name = input("输入用户名或【b】退出：  ")
                if user_name == "b":
                    break
                stu = public.stu_login(user_name)#登陆成功返回stu对象
                if stu:
                    while True:
                        second = ["选择课程", "保存选择的课程", "去上课", "个人参数"]
                        user_chose = public.show(*second)#格式化输出返回用户选择
                        if not user_chose:
                            break
                        elif user_chose == "1":
                            while True:
                                course_obj = maker.chose_course_obj()#提取已经生成的课程对象list
                                if not course_obj:#如果没有课程提示并返回
                                    break
                                else:
                                    maker.show_course(course_obj)#如果有则格式化输出所有课程
                                    # break
                                stu_chose = public.chose_obj()#用户选择要上的课程并返回课程对象
                                if not stu_chose:               #退出
                                    break
                                stu_obj = public.load_stu_obj(user_name)
                                stu_obj.add_course(stu_course)
                                stu.timetable.append(stu_chose)#用户将课程对象添加到自己的课程表中
                                msg = "您已经选择了{}门课".format(len(stu.timetable))
                                public.color_msg(33, msg, 40)
                        elif user_chose == "2":
                            while True:
                                maker.show_course(stu.timetable)
                                # print(stu.timetable)
                                if stu.timetable == []:
                                    msg = "还没有添加课程"
                                    public.color_msg(31, msg, 40)
                                    break
                                stu_save = input("确认课程表 [y/n]")#是否保存选择课程后的学生对象
                                if stu_save == "n":
                                    stu.timetable = []#清空对象中的课程
                                    break
                                elif stu_save == "y":
                                    stu.add_course()
                                    # public.update_stu_obj(stu)#更新用户课程表
                                    stu.timetable = []#使用学生对象方法清空缓存本地课程表
                                    msg = "保存课程成功"
                                    public.color_msg(32,msg,40)
                                    break
                        elif user_chose == "3":
                            while True:
                                show_status = stu.show_course()#先展示已经选的课程               然后去执行选中后上课
                                if not show_status:#没有选择课程就返回
                                    break
                                stu_obj = public.load_stu_obj(user_name)#通过登陆名找到用户对象
                                stu_course = public.stu_course_chose(stu_obj)#让用户选择要上的课程的对象返回出来并从用户选课列表中删除
                                if stu_course == "b":
                                    break
                                elif not stu_course:
                                    continue
                                maker.go_to_school(stu_course, stu)#执行上课功能

                        elif user_chose == "4":
                                # stu_obj = public.load_stu_obj(user_name)
                                # maker.show_stu(stu_obj)
                                stu.show_stu()
                        else:
                            msg = "输入有误"
                            public.color_msg(31, msg, 100)


#主界面不能退出
#找哪里出的问题
students_main()